package com.ds.topmenu.plugins;

import com.alibaba.fastjson.annotation.JSONField;
import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.TreeListResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.client.ESDClient;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTreeViewAnnotation;
import com.ds.esd.custom.toolbar.MenuBarMenu;
import com.ds.esd.dsm.domain.annotation.OrgDomain;
import com.ds.esd.dsm.domain.enums.OrgDomainType;
import com.ds.esd.editor.enums.CustomMenuType;
import com.ds.right.item.RightFormulaTypeItem;
import com.ds.right.item.RightType;
import com.ds.web.annotation.Aggregation;
import com.ds.web.annotation.AggregationType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/action/right/")
@MethodChinaName(cname = "权限管理")
@MenuBarMenu(menuType = CustomMenuType.top, caption = "权限管理", index = 6, imageClass = "bpmfont bpmgongzuoliu")
//插件显示位置
@Aggregation(type = AggregationType.customDomain)//声明为Domin域
@OrgDomain(type = OrgDomainType.right)//构建当前域环境
public class RightIndex {

    public TreeListResultModel<List<RightFormulaTypeItem>> getIndex(String id, String projectName, String className, RightType rightType) {
        TreeListResultModel<List<RightFormulaTypeItem>> resultModel = new TreeListResultModel<List<RightFormulaTypeItem>>();
        List<RightFormulaTypeItem> items = new ArrayList<>();

        RightFormulaTypeItem root = null;
        if (rightType != null) {
            root = new RightFormulaTypeItem(rightType, projectName, className);
        } else {
            root = new RightFormulaTypeItem(className, projectName);
        }

        items.add(root);
        if (id != null && !id.equals("")) {
            String[] orgIdArr = StringUtility.split(id, ";");
            resultModel.setIds(Arrays.asList(orgIdArr));
        } else {
            resultModel.setIds(Arrays.asList(new String[]{root.getFristClassItem(root).getId()}));
        }
        resultModel.setData(items);
        return resultModel;

    }


    @MethodChinaName(cname = "组件授权")
    @RequestMapping(method = RequestMethod.POST, value = "ComRight")
    @NavTreeViewAnnotation
    @DialogAnnotation()
    @ModuleAnnotation(width = "750", height = "600", dynLoad = true, imageClass = "spafont spa-icon-astext", caption = "组件授权")
    @CustomAnnotation(imageClass = "spafont spa-icon-astext")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_projectName, RequestPathEnum.SPA_className})
    @ResponseBody
    public TreeListResultModel<List<RightFormulaTypeItem>> getComRight(String id, String projectName, String className) {
        return getIndex(id, projectName, className, RightType.comright);
    }

    @MethodChinaName(cname = "模块授权")
    @RequestMapping(method = RequestMethod.POST, value = "ModuleRight")
    @NavTreeViewAnnotation
    @DialogAnnotation
    @CustomAnnotation(imageClass = "spafont spa-icon-moveforward")
    @ModuleAnnotation(width = "750", height = "600", dynLoad = true, imageClass = "spafont spa-icon-moveforward", caption = "模块授权")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_projectName, RequestPathEnum.SPA_className})
    @ResponseBody
    public TreeListResultModel<List<RightFormulaTypeItem>> getModuleRight(String id, String projectName, String className) {
        return getIndex(id, projectName, className, RightType.moduleright);
    }

    @MethodChinaName(cname = "流程授权")
    @RequestMapping(method = RequestMethod.POST, value = "BpmRight")
    @NavTreeViewAnnotation
    @DialogAnnotation
    @CustomAnnotation(imageClass = "bpmfont bpmgongzuoliuzhutiguizeweihuguanli")
    @ModuleAnnotation(width = "750", height = "600", dynLoad = true, imageClass = "bpmfont bpmgongzuoliuzhutiguizeweihuguanli", caption = "流程授权")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_projectName, RequestPathEnum.SPA_className})
    @ResponseBody
    public TreeListResultModel<List<RightFormulaTypeItem>> getBpmRight(String id, String projectName, String className) {
        return getIndex(id, projectName, className, RightType.bpmright);
    }

//
//    @MethodChinaName(cname = "授权管理")
//    @RequestMapping(method = RequestMethod.POST, value = "TabIndex")
//    @NavTabsViewAnnotation
//    @DialogAnnotation
//    @ModuleAnnotation(width = "750", height = "600", dynLoad = true, imageClass = "bpmfont bpmgongzuoliu", caption = "授权管理")
//    @ResponseBody
//    public TreeListResultModel<List<RightManagerTypeItem>> getTabIndex(String id, String projectName) {
//        TreeListResultModel<List<RightManagerTypeItem>> resultModel = new TreeListResultModel<List<RightManagerTypeItem>>();
//        List<RightManagerTypeItem> items = new ArrayList<>();
//        RightManagerTypeItem<RightManagerTypeItem> root = new RightManagerTypeItem(projectName);
//        for (RightManagerTypeItem item : root.getSub()) {
//            items.add(item);
//        }
//
//        if (id != null && !id.equals("")) {
//            String[] orgIdArr = StringUtility.split(id, ";");
//            resultModel.setIds(Arrays.asList(orgIdArr));
//        } else {
//            resultModel.setIds(Arrays.asList(new String[]{root.getFristClassItem(root).getId()}));
//        }
//        resultModel.setData(items);
//        return resultModel;
//
//    }


    @JSONField(serialize = false)
    public ESDClient getEsdClient() throws JDSException {

        ESDClient client = ESDFacrory.getESDClient();

        return client;
    }
}
